//package com.open.party.cloud.view.home.dangYuanJiaoYu
//
//import android.annotation.SuppressLint
//import android.content.pm.ActivityInfo
//import android.content.res.Configuration
//import android.os.Build
//import android.os.Bundle
//import android.os.Parcel
//import android.os.Parcelable
//import android.text.TextUtils
//import android.view.View
//import android.view.ViewGroup
//import android.view.WindowManager
//import android.widget.LinearLayout
//import android.widget.Toast
//import cn.android.x.model.data.ResultInfo
//import cn.android.x.parent.AppEventBusRecycleViewTitleBaseActivity
//import com.kk.taurus.playerbase.assist.InterEvent
//import com.kk.taurus.playerbase.assist.OnVideoViewEventHandler
//import com.kk.taurus.playerbase.entity.DataSource
//import com.kk.taurus.playerbase.event.OnPlayerEventListener
//import com.kk.taurus.playerbase.event.OnPlayerEventListener.PLAYER_EVENT_ON_PLAY_COMPLETE
//import com.kk.taurus.playerbase.player.IPlayer
//import com.kk.taurus.playerbase.receiver.ReceiverGroup
//import com.kk.taurus.playerbase.widget.BaseVideoView
//import com.open.party.cloud.R
//import com.open.party.cloud.model.YunKePlayBean
//import com.open.party.cloud.view.home.dangYuanJiaoYu.adapter.LiangDuYunKeTangPlayListAdapter
//import com.sinothk.android.utils.XUtils
//import com.sinothk.manager.video.DataInter
//import com.sinothk.manager.video.PUtil
//import com.sinothk.manager.video.ReceiverGroupManager
//import com.sinothk.manager.video.bean.VideoSourceEntity
//import com.sinothk.rxretrofit.bean.PageVo
//import kotlinx.android.synthetic.main.video_play_activity.*
//import org.greenrobot.eventbus.Subscribe
//import org.greenrobot.eventbus.ThreadMode
//
///**
// * 课程视频播放详情页面
// */
//class YunKeTangStudyVideoActivityB() : AppEventBusRecycleViewTitleBaseActivity<YunKePlayBean>(), OnPlayerEventListener, Parcelable {
//
//    private var userPause: Boolean = false
//    private var isLandscape: Boolean = false
//    private var margin: Int = 0
//    private var mReceiverGroup: ReceiverGroup? = null
//    private var dataSource = DataSource()
//    private var videoSource: VideoSourceEntity? = null
//
//    //    private var courseDetailEntity: CourseDetailEntity? = null
//    private var curPosition = 0
//    private var isVideo = false
//    private var isPdf = false
//
//    //    private var courseListAdapter: CourseListAdapter? = null
////    private var commPresenter: CommPresenter? = null
////    private var tab2Presenter: Tab2Presenter? = null
//    private var videoId: String = ""
//    private var startTime: String = ""
//    private var timeStart: Long = 0
//    private var endTime: String = ""
//    private var timeEnd: Long = 0
//    private var isPlay: Boolean = true
//
//    override fun getLayoutResId(): Int = R.layout.video_play_activity
//
//    override fun onCreate(savedInstanceState: Bundle?) {
//        super.onCreate(savedInstanceState)
//        window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
//        window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
//        window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN)
//
//        setViewTitle("课程学习")
//
//        init()
//    }
//
//    private fun init() {
//        videoSource = intent.getSerializableExtra("DataSource") as VideoSourceEntity
////        courseDetailEntity = intent.getSerializableExtra("entity") as CourseDetailEntity
//        curPosition = intent.getIntExtra("position", 0)
//        videoId = intent.getStringExtra("videoId") as String
//
////        commPresenter = CommPresenter(this)
////        tab2Presenter = Tab2Presenter(this)
////        courseListAdapter = CourseListAdapter(this, true)
////        courseListView.adapter = courseListAdapter
////        courseListAdapter!!.data = courseDetailEntity!!.cmList
////        courseListAdapter!!.setCurPlayItemView(curPosition)
//
//        margin = PUtil.dip2px(this, 2f)
//        updateVideo(false)
//        mReceiverGroup = ReceiverGroupManager.get().getReceiverGroup(this)
//        mReceiverGroup!!.groupValue.putBoolean(DataInter.Key.KEY_CONTROLLER_TOP_ENABLE, true)
//        baseVideoView.setReceiverGroup(mReceiverGroup)
//        baseVideoView.setEventHandler(onVideoViewEventHandler)
//        baseVideoView.setOnPlayerEventListener(this)
//
////        courseListAdapter!!.setOnAdapterItemClickListener { position, data ->
////            if (curPosition == position)
////                return@setOnAdapterItemClickListener
////            if ("视频" == data.type.desc || "音频" == data.type.desc) {
////                isVideo = true
////                isPdf = false
////                curPosition = position
////            } else {
////                isVideo = false
////                isPdf = true
////            }
////            learnFileId = data.id
////            commPresenter!!.queryFile(arrayOf(data.id), arrayOf(Biz.COURSEWARE_MANAGEMENT_DATA))
////        }
//
//        initRecycleView()
//    }
//
////    override fun loadingShow() {
////    }
////
////    override fun loadingDismiss() {
////    }
////
////    override fun showTip(msg: String?) {
////    }
////
////    override fun loadComplete(resultData: Any?) {
////        if (resultData is LearnRecordEntity) {
////            return
////        }
////        val queryFileEntityList = resultData as List<QueryFileEntity>
////        if (queryFileEntityList.isNotEmpty()) {
////            val fileEntity = queryFileEntityList[0].list[0]
////            when {
////                isVideo -> {
////                    courseListAdapter!!.setCurPlayItemView(curPosition)
////                    val videoSourceEntity = VideoSourceEntity()
////                    videoSourceEntity.title = fileEntity.submittedFileName
////                    videoSourceEntity.url = fileEntity.url
////                    videoSource = videoSourceEntity
////                    initPlay()
////                }
////                isPdf -> {
////                    val learnRecordEntity = LearnRecordEntity()
////                    learnRecordEntity.cmId = courseDetailEntity!!.id
////                    learnRecordEntity.learnId = learnFileId
////                    learnRecordEntity.learningStaff = DataCache.getUserInfo().entityId
////                    learnRecordEntity.isState = true
////                    val time = DateUtil.getDateStrByDate(DateUtil.getCurTimeDate())
////                    learnRecordEntity.startTime = time
////                    learnRecordEntity.endTime = time
////                    learnRecordEntity.learningDuration = "0"
////                    tab2Presenter!!.uploadStudyRecord(learnRecordEntity)
////                    WebPageActivity.start(this@VideoPlayActivity, "学习资料", getFileView(fileEntity.url))
////                }
////            }
////        } else {
////            if (isVideo || isPdf) {
////                ToastUtil.show("未找到课件")
////            }
////        }
////    }
//
////    private fun getFileView(url: String?): String {
////        return "http://www.xdocin.com/xdoc?_func=to&amp;_format=html&_cache=1&_xdoc=$url"
////    }
//
////    override fun load2Complete(resultData: Any?) {
////    }
//
//    private fun initPlay() {
//        baseVideoView.rePlay(0)
//        baseVideoView.setDataSource(getDataSource())
//        baseVideoView.start()
//        startTime = XUtils.date().getDateStrByDate(XUtils.date().curTimeDate)
//        timeStart = XUtils.date().time
//    }
//
//    private fun updateVideo(landscape: Boolean) {
//        val layoutParams = baseVideoView.layoutParams as LinearLayout.LayoutParams
//        if (landscape) {
//            titleBarView.visibility = View.GONE
//            layoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT
//            layoutParams.height = ViewGroup.LayoutParams.MATCH_PARENT
//            layoutParams.setMargins(0, 0, 0, 0)
//            hideBottomUIMenu2()
//        } else {
//            titleBarView.visibility = View.VISIBLE
//            layoutParams.width = PUtil.getScreenW(this)
//            layoutParams.height = layoutParams.width * 9 / 16
//            layoutParams.setMargins(0, 0, 0, 0)
//            showBottomUIMenu2()
//        }
//        baseVideoView.layoutParams = layoutParams
//    }
//
//    @SuppressLint("ObsoleteSdkInt")
//    private fun hideBottomUIMenu2() {
//        //隐藏虚拟按键
//        if (Build.VERSION.SDK_INT in 12..18) {
//            val v = window.decorView
//            v.systemUiVisibility = View.GONE
//        } else if (Build.VERSION.SDK_INT >= 19) {
//            val decorView = window.decorView
//            val uiOptions = (View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
//                    or View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
//                    or View.SYSTEM_UI_FLAG_FULLSCREEN)
//            decorView.systemUiVisibility = uiOptions
//        }
//    }
//
//    /**
//     * 显示菜单栏
//     * 如果底部的bar 隐藏就显示
//     */
//    @SuppressLint("ObsoleteSdkInt")
//    private fun showBottomUIMenu2() {
//        // 显示虚拟按键
//        if (Build.VERSION.SDK_INT in 12..18) {
//            //低版本sdk
//            val v = window.decorView
//            v.systemUiVisibility = View.VISIBLE
//        } else if (Build.VERSION.SDK_INT >= 19) {
//            val decorView = window.decorView
//            val uiOptions = View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
//            decorView.systemUiVisibility = uiOptions
//        }
//    }
//
//    private val onVideoViewEventHandler = object : OnVideoViewEventHandler() {
//        override fun onAssistHandle(assist: BaseVideoView?, eventCode: Int, bundle: Bundle?) {
//            super.onAssistHandle(assist, eventCode, bundle)
//            when (eventCode) {
//                InterEvent.CODE_REQUEST_PAUSE -> userPause = true
//                DataInter.Event.EVENT_CODE_REQUEST_BACK -> if (isLandscape) {
//                    requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
//                } else {
//                    finish()
//                }
//                DataInter.Event.EVENT_CODE_REQUEST_TOGGLE_SCREEN -> requestedOrientation = if (isLandscape)
//                    ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
//                else
//                    ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
//                DataInter.Event.EVENT_CODE_ERROR_SHOW -> baseVideoView.stop()
//            }
//        }
//
//        override fun requestRetry(videoView: BaseVideoView, bundle: Bundle?) {
//            if (PUtil.isTopActivity(this@YunKeTangStudyVideoActivityB)) {
//                super.requestRetry(videoView, bundle)
//            }
//        }
//    }
//
//    constructor(parcel: Parcel) : this() {
//        userPause = parcel.readByte() != 0.toByte()
//        isLandscape = parcel.readByte() != 0.toByte()
//        margin = parcel.readInt()
//        curPosition = parcel.readInt()
//        isVideo = parcel.readByte() != 0.toByte()
//        isPdf = parcel.readByte() != 0.toByte()
//        videoId = parcel.readString()!!
//        startTime = parcel.readString()!!
//        timeStart = parcel.readLong()
//        endTime = parcel.readString()!!
//        timeEnd = parcel.readLong()
//        isPlay = parcel.readByte() != 0.toByte()
//    }
//
//    override fun onConfigurationChanged(newConfig: Configuration) {
//        super.onConfigurationChanged(newConfig)
//        if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
//            isLandscape = true
//            updateVideo(true)
//        } else {
//            isLandscape = false
//            updateVideo(false)
//        }
//        mReceiverGroup!!.groupValue.putBoolean(DataInter.Key.KEY_IS_LANDSCAPE, isLandscape)
//    }
//
//    override fun onPause() {
//        super.onPause()
//        isPlay = false
//
//        endTime = XUtils.date().getDateStrByDate(XUtils.date().curTimeDate)
//        timeEnd = XUtils.date().time
//        updateLearnRecord(false)
//        if (baseVideoView.isInPlaybackState) {
//            baseVideoView.pause()
//        } else {
//            baseVideoView.stop()
//        }
//    }
//
//    override fun onResume() {
//        super.onResume()
//        val state = baseVideoView.state
//        if (state == IPlayer.STATE_PLAYBACK_COMPLETE) {
//            endTime = XUtils.date().getDateStrByDate(XUtils.date().curTimeDate)
//            timeEnd = XUtils.date().time
//            updateLearnRecord(false)
//            return
//        }
//        if (baseVideoView.isInPlaybackState) {
//            if (!userPause)
//                baseVideoView.resume()
//        } else {
//            baseVideoView.rePlay(0)
//        }
//        initPlay()
//    }
//
//    override fun onDestroy() {
//        super.onDestroy()
//        baseVideoView.stopPlayback()
//    }
//
//    override fun onBackPressed() {
//        if (isLandscape) {
//            requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
//            return
//        }
//        super.onBackPressed()
//    }
//
//    private fun updateLearnRecord(isPlayComplete: Boolean) {
////        val learnRecordEntity = LearnRecordEntity()
////        learnRecordEntity.cmId = learnFileId
////        learnRecordEntity.learnId = courseDetailEntity!!.id
////        learnRecordEntity.learningStaff = DataCache.getUserInfo().entityId
////        learnRecordEntity.isState = isPlayComplete
////        learnRecordEntity.startTime = startTime
////        learnRecordEntity.endTime = endTime
////        learnRecordEntity.learningDuration = "${baseVideoView.currentPosition / 1000}"
////        tab2Presenter!!.uploadStudyRecord(learnRecordEntity)
//    }
//
//    override fun onPlayerEvent(eventCode: Int, bundle: Bundle?) {
//        if (eventCode == PLAYER_EVENT_ON_PLAY_COMPLETE && isPlay) {
//            endTime = XUtils.date().getDateStrByDate(XUtils.date().curTimeDate)
//            timeEnd = XUtils.date().time
//            updateLearnRecord(true)
//        }
//    }
//
//    private fun getDataSource(): DataSource? {
//        if (videoSource == null || TextUtils.isEmpty(videoSource!!.title) || TextUtils.isEmpty(videoSource!!.url)) {
//            finish()
//            Toast.makeText(this, "标题、地址不能为空", Toast.LENGTH_SHORT).show()
//            return null
//        }
//
//        dataSource.title = videoSource!!.title
//        dataSource.data = videoSource!!.url
//        return dataSource
//    }
//
//    override fun writeToParcel(parcel: Parcel, flags: Int) {
//        parcel.writeByte(if (userPause) 1 else 0)
//        parcel.writeByte(if (isLandscape) 1 else 0)
//        parcel.writeInt(margin)
//        parcel.writeInt(curPosition)
//        parcel.writeByte(if (isVideo) 1 else 0)
//        parcel.writeByte(if (isPdf) 1 else 0)
//        parcel.writeString(videoId)
//        parcel.writeString(startTime)
//        parcel.writeLong(timeStart)
//        parcel.writeString(endTime)
//        parcel.writeLong(timeEnd)
//        parcel.writeByte(if (isPlay) 1 else 0)
//    }
//
//    override fun describeContents(): Int {
//        return 0
//    }
//
//    companion object CREATOR : Parcelable.Creator<YunKeTangStudyVideoActivityB> {
//        override fun createFromParcel(parcel: Parcel): YunKeTangStudyVideoActivityB {
//            return YunKeTangStudyVideoActivityB(parcel)
//        }
//
//        override fun newArray(size: Int): Array<YunKeTangStudyVideoActivityB?> {
//            return arrayOfNulls(size)
//        }
//    }
//
//    // 下面列表
//    private var adapter: LiangDuYunKeTangPlayListAdapter? = null
//
//    private fun initRecycleView() {
//        initRecycleLinearView(loadingRecyclerView)
//
//        adapter = LiangDuYunKeTangPlayListAdapter(this)
//        loadingRecyclerView.adapter = adapter
//
//        adapter!!.setData(YunKeTangClassListActivity.yunKePlayList)
//
//        loadingRecyclerView.setPullRefreshEnabled(false)
//        loadingRecyclerView.setLoadingMoreEnabled(false)
//
//        adapter!!.setOnSuperListener { position, itemData, _ ->
//
////            if (yunKePlayBean.type != null && yunKePlayBean.type.code != null && yunKePlayBean.type.code != "SP") {
////
////            } else {
////
////            }
//
//            if (itemData.isPlaying) {
//                return@setOnSuperListener
//            }
//
//            val videoSourceEntity = VideoSourceEntity()
//            videoSourceEntity.title = itemData.name
//            videoSourceEntity.url = itemData.fileUrl
//            videoSource = videoSourceEntity
//            initPlay()
//
//            for (index in YunKeTangClassListActivity.yunKePlayList!!.indices) {
//                YunKeTangClassListActivity.yunKePlayList!![index].isPlaying = false
//            }
//            YunKeTangClassListActivity.yunKePlayList!![position].isPlaying = true
//
//            adapter!!.notifyDataSetChanged()
//        }
//    }
//
//    override fun loadData(pageVo: PageVo<YunKePlayBean>?) {
//
//    }
//
//    @Subscribe(threadMode = ThreadMode.MAIN)
//    public fun eventBusCallback(result: ResultInfo<ArrayList<YunKePlayBean>>) {
//
//    }
//}